package com.example.imtoken.modules.main.wallet.select

import android.annotation.SuppressLint
import android.app.Dialog
import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.*
import com.example.imtoken.R
import com.example.imtoken.core.OnSetDefHomeWalletListener
import com.example.imtoken.core.OnSwitchWalletListener
import com.example.imtoken.databinding.SelectWalletFragmentBinding
import com.example.imtoken.modules.main.wallet.manage.wallet.ManageWalletActivity
import com.example.imtoken.modules.main.wallet.manage.wallet.ManageWalletAdapter
import com.example.imtoken.modules.main.wallet.manage.wallet.ManageWalletTypeAdapter
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

class SelectWalletDialogFragment() :
    BottomSheetDialogFragment(), OnSetDefHomeWalletListener {

    private val viewModel: SelectWalletDialogViewModel by lazy {
        ViewModelProvider(this).get(SelectWalletDialogViewModel::class.java)
    }
    private lateinit var dataBinding: SelectWalletFragmentBinding

    private var mBehavior: BottomSheetBehavior<View>? = null

    private lateinit var manageWalletTypeAdapter: ManageWalletTypeAdapter
    private lateinit var manageWalletAdapter: ManageWalletAdapter

    private var switchWallet: OnSwitchWalletListener? = null

    constructor(switchWallet: OnSwitchWalletListener) : this() {
        this.switchWallet = switchWallet
    }

    override fun onResume() {
        super.onResume()
        if (!viewModel.isLoad){
            viewModel.switchWallet.value = switchWallet
            viewModel.isLoad = true
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //重写样式,设置dialog背景色为透明
        setStyle(STYLE_NORMAL, R.style.AppBottomSheet)
    }

    @SuppressLint("ResourceType")
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
        dataBinding =
            DataBindingUtil.inflate(layoutInflater, R.layout.select_wallet_fragment, null, false)
        dialog.setContentView(dataBinding.root)
        val lp = dataBinding.root.layoutParams
        lp.height = ScreenUtils.getAppScreenHeight() - ConvertUtils.sp2px(50f)
        dataBinding.root.layoutParams = lp
        mBehavior = BottomSheetBehavior.from(dataBinding.root.parent as View)
        initViewContent()
        dataBinding.viewModel = viewModel
        return dialog
    }

    private fun initViewContent() {
        initManageWalletTypeAdapter()
        initManageWalletAdapter()
        initObserve()

        viewModel.getManageWalletTypeData()

        dataBinding.ivBlack.setOnClickListener { dismiss() }
        dataBinding.tvManage.setOnClickListener {
            ActivityUtils.startActivity(ManageWalletActivity::class.java)
            this.dismiss()
        }
    }

    private fun initManageWalletAdapter() {
        manageWalletAdapter = ManageWalletAdapter(R.layout.item_manage, 1)
        dataBinding.rvManageWallet.layoutManager = LinearLayoutManager(context)
        dataBinding.rvManageWallet.adapter = manageWalletAdapter
        manageWalletAdapter.setOnSetDefHomeWalletListener(this)
    }

    override fun defAddress(address: String?) {
        if (viewModel.switchHomeWallet(address)) {
            LogUtils.e("切换首页默认钱包成功")
            viewModel.switchWallet.value?.switchWallet()
            dismiss()
        } else {
            LogUtils.e("切换首页默认钱包失败")
            ToastUtils.showShort("error")
        }
    }

    private fun initManageWalletTypeAdapter() {
        manageWalletTypeAdapter = ManageWalletTypeAdapter(R.layout.item_manage_wallet_type)
        dataBinding.rvSelectWallet.layoutManager = LinearLayoutManager(context)
        dataBinding.rvSelectWallet.adapter = manageWalletTypeAdapter
        manageWalletTypeAdapter.setOnItemClickListener { _, _, position ->
            viewModel.updateSelectData(position)
        }
    }

    private fun initObserve() {
        viewModel.getManageWalletTypeLiveData().observe(this) {
            manageWalletTypeAdapter.setList(it)
        }
        viewModel.getManageWalletLiveData().observe(this) {
            manageWalletAdapter.setList(it)
            dataBinding.rvManageWallet.scrollToPosition(0)
        }
    }


    override fun onStart() {
        super.onStart()
        mBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
    }

}